package com.woniu.producer;

import com.woniu.entity.RabbitmqConst;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class DelayProducer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(String routeKey,String msg,Integer ttl){
        rabbitTemplate.convertAndSend(RabbitmqConst.DELAY_EXCHANGE, routeKey, msg, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //指定消息的TTL时间
                message.getMessageProperties().setDelay(ttl);
                return message;
            }
        });
    }
}
